package com.pfzy.ui.activity.nested

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import android.view.ViewConfiguration
import android.widget.LinearLayout
import java.lang.Math.abs

class MyNestScrollView @JvmOverloads constructor(
    val mContext: Context,
    val attributeSet: AttributeSet? = null,
    val flag: Int = 0
) : LinearLayout(mContext, attributeSet, flag) {
    private var mTouchSlop = 0
    private var mStartX = 0f
    private var startY = 0f
    private var mTopViewHeight = 0

    init {
        mTouchSlop = ViewConfiguration.get(context).scaledPagingTouchSlop
    }

    override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
        var intercepted = false

        /**什么时候拦截事件呢，当头部还没有消失的时候*/
        when (ev?.action) {
            MotionEvent.ACTION_DOWN -> {
                startY = ev.rawY
            }
            MotionEvent.ACTION_MOVE -> {
                val endY = ev.rawY
                if (abs(startY - endY) > mTouchSlop) {
                    if (isViewHidden((startY - endY).toInt()) || isViewShow((startY - endY).toInt())) {
                        Log.e("TAG","此时就需要拦截，外部进行消费事件")
                        // 此时就需要拦截，外部进行消费事件
                        intercepted = true
                    }
                }
                startY = endY
            }
        }
        return intercepted
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        when (event?.action) {
            MotionEvent.ACTION_DOWN -> {
                Log.e("TAG", "MyNestScrollView ACTION_DOWN")
                startY = event.y

                mStartX = event.y
                //子View开始嵌套滑动
//                var axis = ViewCompat.SCROLL_AXIS_NONE
//                axis = axis or ViewCompat.SCROLL_AXIS_VERTICAL
//                nestedScrollingChildHelper.startNestedScroll(axis)
            }
            MotionEvent.ACTION_MOVE -> {
                Log.e("TAG", "MyNestScrollView ACTION_MOVE")
                val endY = event.y

                if (abs(endY - startY) > mTouchSlop) {
                    //滑动了
                    scrollBy(0, (startY - endY).toInt())
                }

                startY = endY
            }
        }

        return super.onTouchEvent(event)
    }

    /**
     * 滑动到具体位置
     */
    override fun scrollTo(x: Int, y: Int) {
        var finalY = 0

        if (y < 0) {// 如果一直向下滑，最终totalY也会小于0，所以这里也是做一次边界的处理。

        } else {
            finalY = y
        }

        // 下吸顶效果，所以我们需要知道，顶部View的高度，以便控制滑动的距离，也是一次边界处理
        if (y > mTopViewHeight) {
            finalY = mTopViewHeight
        }

        super.scrollTo(x, finalY)
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        //顶部view是第一个View
        mTopViewHeight = getChildAt(0).measuredHeight
    }

    /**
     * 头部View逐渐消失
     * @param dy 手指滑动的相对距离 dy >0 上滑 dy < 0 下滑
     */
    private fun isViewHidden(dy: Int): Boolean {
        return dy > 0 && scrollY < mTopViewHeight
    }
    private fun isViewShow(dy: Int):Boolean{
        // canScrollVertically 意味着当前View是能够往下滑动的，如果返回了false，那么就是不能继续往下滑动了。
        return dy < 0 && scrollY > 0 && !canScrollVertically(-1)
    }

}
